package com.baosight.sczx.zm.cc.service; 
import java.util.HashMap;
import java.util.List;
import java.util.Map; 
import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo; 
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.cc.OperationHistory;
import com.baosight.sczx.zm.cc.domain.ZMCCProduceproblemHandleProcess_x;
import com.baosight.sczx.zm.cc.domain.ZMCCProduceproblemReview; 
/**
 * 生产问题处理---复查记录
 */
public class ServiceZMCC15 extends ServiceEPBase {
	/**
	 * 初始化 
	 */
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo); 
		return outInfo;
	}
	/**
	 * 查询
	 */
	public EiInfo query(EiInfo inInfo)
	{
		//查询当前记录的数据，进行展示
		String fdRecordId = inInfo.getString("fdRecordId");
		StaticTools.setQueryParameters(inInfo, "fdRecordId", fdRecordId, true);//添加查询条件 
		EiInfo outInfo = super.query(inInfo, "ZMCCProduceproblemHandleProcess_x.query", new ZMCCProduceproblemHandleProcess_x()); 
		return outInfo;
	}
	/**
	 * 新增方法 
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) 
	{
		EiInfo out=new EiInfo(); 
		ZMCCProduceproblemReview _dom = new ZMCCProduceproblemReview();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata); 
		
		_dom.fromMap(eiBlock.getRow(0));
		
		String fdRiskRecordId = _dom.getFdRecordId();//隐患记录ID
		String fdIsRedo = _dom.getFdIsRedo();//是否重新整改
		String fdIsRepeat = eiBlock.getCellStr(0,"fdIsRepeat");//是否重复隐患,该字段不在当前表中
		boolean value = this.canRecheck(fdRiskRecordId);//是否====没有被关闭

		Map<String, String> params1 = new HashMap<String, String>();
		params1.put("fdIsRepeat", fdIsRepeat);
		if(value) {
			if("是".equals(fdIsRedo)) {
				params1.put("fdProblemStatus", "1");//重新整改 更改状态 修改是否重复隐患字段 
				int number = 2;
				if(!"否".equals(fdIsRepeat)) {
					try {
						String _tempNumber = fdIsRepeat.substring(fdIsRepeat.indexOf("(") + 1, fdIsRepeat.indexOf(")"));
						number = Integer.parseInt(_tempNumber) + 1;
					} catch (Exception e) {
						System.out.println("隐患处理模块,填写复查意见异常: "+ e.getMessage());
					} 
					params1.put("fdIsRepeat", "是("+number+")");
				}
				else { 
					params1.put("fdIsRepeat", "是(1)");
				}
			}
			else { 
				params1.put("fdProblemStatus",  "5");
				boolean insertValue = OperationHistory.InsertOperationHistory(_dom.getFdRecordId(), "生产问题","关闭");//新增问题处理记录
				if(!insertValue) {
					inInfo.setStatus(-1);
					inInfo.setMsg("插入操作记录失败！");
					return inInfo;
				}
			}
			int result = this.dao.update("ZMCCProduceproblem.modifyRecheck", params1);
			if(result != -1) {
				//复查表插入
				_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CC_PRODUCEPROBLEM_REVIEW"));
			    this.dao.insert("ZMCCProduceproblemReview.insert", _dom); 
			    //操作记录
			    boolean insertValue = OperationHistory.InsertOperationHistory(_dom.getFdRecordId(), "生产问题","复查");//新增隐患处理记录
				if(!insertValue) {
					inInfo.setStatus(-1);
					inInfo.setMsg("插入操作记录失败！");
					return inInfo;
				}
				//更改处理表
				Map<String, String> _argMap = new HashMap<String, String>();
				_argMap.put("fdKeyId", _dom.getFdRecordHandleId());
				this.dao.update("ZMCCProduceproblemHandle.modifyRechecked", _argMap); 
			}
		}  
			
			
		out = this.query(inInfo); 
		out.setStatus(1);
		out.setMsg("操作记录成功");
		return out; 		
	} 
	
	private boolean canRecheck(String fdKeyId) {
		Map<String, String> _argMap = new HashMap<String, String>();
		_argMap.put("fdRecordId", fdKeyId);
		List<?> _result = dao.query("ZMCCProduceproblemHandle.canRecheck", _argMap);
		if(_result.size() > 0) {
			return true;
		}
		else {
			return false;
		}
	}
}
